package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.LabPolicy;
import com.web.portal.entity.TutoringCenter;
import com.web.portal.exception.PortalException;
import com.web.portal.service.LabPolicyService;

@Controller
public class LabPolicyController {
	@Autowired
	LabPolicyService labPolicyService;

	public LabPolicyService getLabPolicyService() {
		return labPolicyService;
	}

	public void setLabPolicyService(LabPolicyService labPolicyService) {
		this.labPolicyService = labPolicyService;
	}

	@RequestMapping(value = "/admin/labpolicy.do")
	public String getLabPolicy(Map<String, Object> model) throws PortalException {
		LabPolicy labPolicy = new LabPolicy();
		List<LabPolicy> labPolicyList = labPolicyService.getList(labPolicy);
		LabPolicy labP=null;
		if(!labPolicyList.isEmpty()){
			labP=labPolicyList.get(0);
		}
		List<LabPolicy>  labPolicyLists= new ArrayList<LabPolicy>();
		labPolicyLists.add(labP);
		model.put("labPolicyList", labPolicyLists);
		return "lab_policy_view";
	}

	@RequestMapping(value = "/admin/labpolicyform.do", method = RequestMethod.GET)
	public String getLabPolicyForm(Map<String, Object> model) throws PortalException {
		model.put("labPolicy", new LabPolicy());
		return "lab_policy_form";

	}

	@RequestMapping(value = "/admin/labpolicysubmit.do", method = RequestMethod.POST)
	public String addLabPolicyForm(
			@Valid @ModelAttribute("labPoliy") final LabPolicy labPolicy,
			BindingResult bindingResult, Map Model)  {
		if (!(labPolicy == null)) {
			labPolicyService.createOrSave(labPolicy);
			return "redirect:/admin/labpolicy.do";
		}
		return "redirect:/admin/labpolicy.do";
	}

	@RequestMapping(value = "/admin/labpolicyeditform.do", method = RequestMethod.GET)
	public String getEditForm(@RequestParam("id") final String id, Map model) throws PortalException{
		if (!(id == null))  {
			LabPolicy labPolicy = labPolicyService.getLabPolicy(id);
			model.put("labPolicy", labPolicy);
			return "lab_policy_edit";
		}
		return "redirect:/admin/labpolicy.do";
	}

	@RequestMapping(value = "/admin/labpolicyeditsubmit.do", method = RequestMethod.POST)
	public String editLabPolicy(
			@Valid @ModelAttribute("labPolicy") final LabPolicy labPolicy , BindingResult result) throws PortalException {
		if (!(labPolicy == null)) {
			LabPolicy thisLabPolicy = new LabPolicy();
			thisLabPolicy.setPolicyProcedureHeading(labPolicy
					.getPolicyProcedureHeading());
			thisLabPolicy.setCellphonePolicy(labPolicy.getCellphonePolicy());
			thisLabPolicy.setContact(labPolicy.getContact());
			thisLabPolicy.setFireDrills(labPolicy.getFireDrills());
			thisLabPolicy.setId(labPolicy.getId());
			thisLabPolicy
					.setPrinterOperations(labPolicy.getPrinterOperations());
			thisLabPolicy.setSchoolCancellationAndSnowPolicy(labPolicy
					.getSchoolCancellationAndSnowPolicy());
			thisLabPolicy
					.setStudentCoperation(labPolicy.getStudentCoperation());
			/*
			 * save it to the database
			 */
			labPolicyService.createOrSave(thisLabPolicy);
			return "redirect:/admin/labpolicy.do";
		}
		return "redirect:/admin/labpolicy.do";
	}

}
